﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class UserProfilePage : WatiNBase
    {
        #region Constructors

        public UserProfilePage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public UserProfilePage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Links

        public Link EditProfileLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Edit Profile"))); }
        }

        public Link ManagePasswordLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Manage Password"))); }
        }

        public Link ManageProfileLink
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Manage Profile"));
                }
                return IEInstance.Link(Find.ByText("Manage Profile"));
            }
        }

        public Link ProfileUpdateLink
        {
            get
            {
                return ContentPane.Link(Find.ById(s => s.EndsWith("ManageUsers_Profile_cmdUpdate")));
            }
        }

        public Link UploadFileLink
        {
            get { return ContentPane.Link(Find.ByText(s => s.Contains("Upload File"))); }
        }

        public Link ImagePropertyUploadFileLink
        {
            get { return ContentPane.Span(Find.ById(s => s.EndsWith("ImagePropertyFileControl"))).Link(Find.ByText(s => s.Contains("Upload File"))); }
        }

        public Link ImagePropertySaveFileLink
        {
            get { return ContentPane.Span(Find.ById(s => s.EndsWith("ImagePropertyFileControl"))).Link(Find.ByText(s => s.Contains("Save File"))); }
        }

        public Link ChangePasswordLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Change Password"));
                }
                return IEInstance.Link(Find.ByTitle("Change Password"));
            }
        }

        public Link SaveFileLink
        {
            get { return ContentDiv.Span(Find.ById(s => s.EndsWith("PhotoFileControl"))).Link(Find.ByText(s => s.Contains("Save File"))); }
        }

        public Link ManageServicesLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Manage Services"))); }
        }

        public Link HTMLEditorModeLink
        {
            get { return ProfilePropertiesDiv.Div(Find.ByClass("reEditorModes")).Link(Find.ByTitle("HTML")); }
        }

        public Link LoadDefaultProfileTemplateLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("ModuleSettings_Settings_cmdLoadDefault")));
                }
                return ContentDiv.Link(Find.ById(s => s.EndsWith("ModuleSettings_Settings_cmdLoadDefault")));
            }
        }

        #endregion

        #region TextFields 

        public TextField UserStreetField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$Street")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$Street")));
            }
        }
        public TextField TextPropertyField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$TextProperty")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$TextProperty")));
            }
        }

        public TextField DateTimeDateField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$DateTimePropertydate")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$DateTimePropertydate")));
            }
        }

        public TextField RegionPropertyField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$RegionProperty")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$RegionProperty")));
            }
        }
      
        public TextField PrefixField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Prefix"))); }
        }

        public TextField MiddleNameField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$MiddleName"))); }
        }

        public TextField SuffixField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Suffix"))); }
        }

        public TextField UnitField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Unit"))); }
        }

        public TextField StreetField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Street"))); }
        }

        public TextField CityField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$City"))); }
        }

        public TextField RegionField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Region"))); }
        }

        public TextField PostalCodeField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$PostalCode"))); }
        }

        public TextField TelephoneField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Telephone"))); }
        }

        public TextField CellPhoneField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Cell"))); }
        }

        public TextField FaxField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Fax"))); }
        }

        public TextField WebsiteField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$Website"))); }
        }

        public TextField IMField
        {
            get { return ProfilePropertiesDiv.TextField(Find.ByName(s => s.EndsWith("$IM"))); }
        }
        public TextField ProfileTemplateTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ModuleSettings_Settings_txtTemplate")));
                }
                return ContentDiv.TextField(Find.ById(s => s.EndsWith("ModuleSettings_Settings_txtTemplate")));
            }
        }
        public TextField htmlFieldHost
        {
            get { return HTMLHostFrame.TextField(Find.Any); }
        }
        public TextField htmlFieldAdmin
        {
            get { return HTMLAdminFrame.TextField(Find.Any); }
        }
        public TextField htmlField
        {
            get { return HTMLFrame.TextField(Find.Any); }
        }
        public TextField biographyField
        {
            get { return ContentPane.TextField(Find.ById(s => s.EndsWith("BiographyeditContentHiddenTextarea"))); }
        }
        public TextField CurrentPasswordField
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("_Password_txtOldPassword")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("_Password_txtOldPassword")));
            }
        }

        public TextField NewPasswordField
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("_Password_txtNewPassword")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("_Password_txtNewPassword")));
            }
        }

        public TextField ConfirmNewPasswordField
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("_Password_txtNewConfirm")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("_Password_txtNewConfirm")));
            }
        }
        #endregion

        #region Divs
        public Div ProfileStreetDiv
        {
            get { return IEInstance.Div(Find.ByClass("ProfileAddress")).Divs[1]; }
        }
        public Div ProfilePropertiesDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("ManageUsers_Profile_ProfileProperties"))); }
        }
        public Div ProfileAddressDiv
        {
            get { return ContentDiv.Div(Find.ByClass("ProfileAddress")); }
        }
        public Div ProfileContactInfoDiv
        {
            get { return ContentDiv.Div(Find.ByClass("ProfileContact")); }
        }
        public Div ProfileBioDiv
        {
            get { return ContentDiv.Div(Find.ByClass("ProfileBio")); }
        }
        #endregion

        #region SelectLists
        public SelectList HourSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyhours")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyhours")));
            }
        }

        public SelectList MinuteSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyminutes")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyminutes")));
            }
        }

        public SelectList AmPmSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyampm")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$DateTimePropertyampm")));
            }
        }

        public SelectList CountryPropertySelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$CountryProperty")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$CountryProperty")));
            }
        }
        public SelectList ImageFolderSelectList
        {
            get { return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$ImagePropertyFileControl$Folder"))); }
        }

        public SelectList ImageFileSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ImagePropertyFileControl_File")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ImagePropertyFileControl_File")));
            }
        }

        public SelectList FileSelectList
        {
            get { return IEInstance.SelectList(Find.ByName(s => s.EndsWith("ImagePropertyFileControl$File"))); }
        }

        public SelectList PreferredLocaleSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$PreferredLocale")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$PreferredLocale")));
            }
        }

        public SelectList TimeZoneSelectList
        {
            get { return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$TimeZone"))); }
        }

        public SelectList CountrySelectList
        {
            get { return ProfilePropertiesDiv.SelectList(Find.ByName(s => s.EndsWith("$Country"))); }
        }
        public SelectList PhotoFolderSelectList
        {
            get { return ProfilePropertiesDiv.SelectList(Find.ById(s => s.EndsWith("_PhotoFileControl_Folder"))); }
        }

        public SelectList PhotoFileSelectList
        {
            get { return ProfilePropertiesDiv.SelectList(Find.ById(s => s.EndsWith("_PhotoFileControl_File"))); }
        }
        #endregion

        #region File Uploads

        public FileUpload ImagePropertyFileUpload
        {
            get { return ContentPane.FileUpload(Find.ByName(s => s.Contains("$ImagePropertyFileControl$"))); }
        }

       
        public FileUpload FileUpload
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.FileUpload(Find.ByName(s => s.Contains("$PhotoFileControl$")));
                }
                return IEInstance.FileUpload(Find.ByName(s => s.Contains("$PhotoFileControl$")));
            }
        }

        #endregion

        #region Collections
        //public ImageCollection RequiredImages
        //{
        //    get { return IEInstance.Images.Filter(Find.BySrc(s => s.EndsWith("required.gif"))); }
        //}

        public SpanCollection RequiredPropertiesSpans
        {
            get { return IEInstance.Spans.Filter(Find.ById(s => s.EndsWith("_Req"))); }
        }

        public ElementCollection RequiredElements
        {
            get { return IEInstance.Elements.Filter(Find.ByClass("dnnFormRequired")); }
        }
        #endregion

        #region Tables
        public Table ServicesTable
        {
            get { return IEInstance.Table(Find.ById("dnn_ctr_ManageUsers_MemberServices_grdServices")); }
        }        
        public Table UserCredentialsTable
        {
            get { return ContentDiv.Table(Find.ById("dnn_ctr_ManageUsers_User_UserEditor_tbl")); }
        }
        public Table UserMembershipTable
        {
            get { return ContentDiv.Table(Find.ById("dnn_ctr_ManageUsers_Membership_MembershipEditor_tbl")); }
        }
        public Table PropertiesTable
        {
            get { return ContentPane.Table(Find.ById("dnn_ctr_ManageUsers_Profile_ProfileProperties_tbl")); }
        }
        
        #endregion

        #region Frames
        public Frame HTMLHostFrame
        {
            get { return IEInstance.Frames[3]; }
        }
        public Frame HTMLAdminFrame
        {
            get { return IEInstance.Frames[3]; }
        }
        public Frame HTMLFrame
        {
            get { return IEInstance.Frames[1]; }
        }
        #endregion

        #region Images
        public Image ProfileImage
        {
            get { return ContentDiv.Image(Find.ByAlt("Profile Avatar")); }
        }
        #endregion

        #endregion

        #region Public Methods

        public  void FillInProfileFieldsHost(string photoPath, string prefix, string middle, string suffix, string unit, string street, string city, string region, string postalCode, string telephone, string website, string IM, string biography)
        {
            PrefixField.Value = prefix;
            MiddleNameField.Value = middle;
            SuffixField.Value = suffix;
            UnitField.Value = unit;
            StreetField.Value = street;
            CityField.Value = city;
            CountrySelectList.SelectByValue("Belgium");
            System.Threading.Thread.Sleep(2500);
            RegionField.Value = region;
            PostalCodeField.Value = postalCode;
            TelephoneField.Value = telephone;
            CellPhoneField.Value = telephone;
            FaxField.Value = telephone;
            WebsiteField.Value = website;
            IMField.Value = IM;
            HTMLEditorModeLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            Gallio.Framework.TestLog.WriteLine("There are " + IEInstance.Frames.Count + " Frames");
            htmlFieldHost.Value = biography;
            System.Threading.Thread.Sleep(1500);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            UploadFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            FileUpload.Set(photoPath);
            System.Threading.Thread.Sleep(2000);
            SaveFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
        }

        public void FillInProfileFieldsAdmin(string photoPath, string prefix, string middle, string suffix, string unit, string street, string city, string region, string postalCode, string telephone, string website, string IM, string biography)
        {
            PrefixField.Value = prefix;
            MiddleNameField.Value = middle;
            SuffixField.Value = suffix;
            UnitField.Value = unit;
            StreetField.Value = street;
            CityField.Value = city;
            CountrySelectList.SelectByValue("Belgium");
            System.Threading.Thread.Sleep(2500);
            RegionField.Value = region;
            PostalCodeField.Value = postalCode;
            TelephoneField.Value = telephone;
            CellPhoneField.Value = telephone;
            FaxField.Value = telephone;
            WebsiteField.Value = website;
            IMField.Value = IM;
            HTMLEditorModeLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            Gallio.Framework.TestLog.WriteLine("There are " + IEInstance.Frames.Count + " Frames");
            htmlFieldAdmin.Value = biography;
            System.Threading.Thread.Sleep(1500);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            UploadFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            FileUpload.Set(photoPath);
            System.Threading.Thread.Sleep(2000);
            SaveFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
        }
        public void FillInProfileFields(string photoPath, string prefix, string middle, string suffix, string unit, string street, string city, string region, string postalCode, string telephone, string website, string IM, string biography)
        {
            PrefixField.Value = prefix;
            MiddleNameField.Value = middle;
            SuffixField.Value = suffix;
            UnitField.Value = unit;
            StreetField.Value = street;
            CityField.Value = city;
            CountrySelectList.SelectByValue("Belgium");
            System.Threading.Thread.Sleep(2500);
            RegionField.Value = region;
            PostalCodeField.Value = postalCode;
            TelephoneField.Value = telephone;
            CellPhoneField.Value = telephone;
            FaxField.Value = telephone;
            WebsiteField.Value = website;
            IMField.Value = IM;
            HTMLEditorModeLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            Gallio.Framework.TestLog.WriteLine("There are " + IEInstance.Frames.Count + " Frames");
            htmlField.Value = biography;
            System.Threading.Thread.Sleep(1500);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            UploadFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            FileUpload.Set(photoPath);
            System.Threading.Thread.Sleep(2000);
            SaveFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
            ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);
        }

        #endregion
    }
}